package com.fengke.cn.yejian11.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.fengke.cn.yejian11.R;
import com.fengke.cn.yejian11.activity.AboutYueJianActivity;
import com.fengke.cn.yejian11.activity.UserChangeActivity;
import com.fengke.cn.yejian11.activity.UserDingDanActivity;
import com.fengke.cn.yejian11.activity.UserGuanZhuActivity;
import com.fengke.cn.yejian11.activity.UserShaiTuActivity;
import com.fengke.cn.yejian11.base.BaseActivity;
import com.fengke.cn.yejian11.base.BaseFragment;
import com.fengke.cn.yejian11.config.Config;
import com.fengke.cn.yejian11.impls.ActImpls;
import com.fengke.cn.yejian11.javabean.UserInfo;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class MeFragment extends BaseFragment {


    private TextView user_name;
    private TextView user_shaitunumber;
    private TextView user_dingdannumber;
    private TextView user_guanzhunumber;
    private ImageView user_pic;
    private TextView change_button;
    private UserInfo userInfo;
    private TextView about_yuejian;
//    private String address;
//    private String name;

    public MeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_me, container, false);

        about_yuejian= (TextView) view.findViewById(R.id.user_aboutyuejian);
        user_name= (TextView) view.findViewById(R.id.me_name);
        user_shaitunumber= (TextView) view.findViewById(R.id.me_shaitu);
        user_dingdannumber= (TextView) view.findViewById(R.id.me_dingdan);
        user_guanzhunumber= (TextView) view.findViewById(R.id.me_guanzhu);
        user_pic= (ImageView) view.findViewById(R.id.me_pic);
        change_button= (TextView) view.findViewById(R.id.textView24);
        final String userid=getUserId();
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(Config.URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ActImpls service=retrofit.create(ActImpls.class);
        retrofit2.Call<UserInfo> call=service.getUserIndo(userid);
        call.enqueue(new Callback<UserInfo>() {
            @Override
            public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {
                userInfo=response.body();
//                address=Config.PIC_URL+userInfo.getPhoto();
//                name=userInfo.getUser_name();


                user_name.setText(userInfo.getUser_name());
                user_shaitunumber.setText(userInfo.getShaitunum());
                user_dingdannumber.setText(userInfo.getFocusnum());
                user_guanzhunumber.setText(userInfo.getMessagenum());

                if (userInfo.getPhoto()!=null){
                Picasso.with(getContext()).load(Config.PIC_URL+userInfo.getPhoto()).into(user_pic);
                }else {
                    Picasso.with(getContext()).load(R.drawable.bg_login).into(user_pic);
                }
            }

            @Override
            public void onFailure(Call<UserInfo> call, Throwable t) {

            }
        });

        user_shaitunumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getContext(),UserShaiTuActivity.class);
                intent.putExtra("User_Id",userid);
                intent.putExtra("User_Name",userInfo.getUser_name());
                intent.putExtra("User_Pic",userInfo.getPhoto());
                getContext().startActivity(intent);
            }
        });

        user_guanzhunumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getContext(), UserGuanZhuActivity.class);
                intent.putExtra("User_Id",userid);
                getContext().startActivity(intent);
            }
        });
        user_dingdannumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getContext(), UserDingDanActivity.class);
                intent.putExtra("User_Id",userid);
                getContext().startActivity(intent);
            }
        });
        change_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getContext(), UserChangeActivity.class);
                getContext().startActivity(intent);
            }
        });
        about_yuejian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getContext(), AboutYueJianActivity.class);
                getContext().startActivity(intent);
            }
        });

        return view;
    }

}
